// 路由导航子路由
<template>
  <div>
    <div class="router-item-content" v-if="compare(nowDeep, level)">
      <div @click="turnTo()">{{ path.name }}</div>
      <div @click="deepRouter()" class="deep-btn" v-if="path.childPath != null && path.childPath.length != 0">➡︎</div>
    </div>

    <!-- 还有子节点 -->
    <template v-if="path.childPath != null">
      <div v-for="(item,index) in path.childPath" :key="index">
        <RouterItem :path="item" :nowDeep="nowDeep + index" :level="level" @pathDeep="pathDeep"
                    @routerPush="routerPush"/>
      </div>
    </template>

  </div>
</template>

<script>
export default {
  name: 'RouterItem',
  mounted() {
    console.log("nowDeep: " + this.nowDeep)
  },
  props: {
    path: Object, // 存储该节点的路由总信息
    level: String,  // 用于控制显示的层级
    nowDeep: {
      type: String,
      default: '0'
    },
  },
  data() {
    return {}
  },
  methods: {
    turnTo() { // 跳转前往指定路由
      this.$emit("routerPush", {path: this.path.path})
    },

    routerPush(item) { // 用于递归向上传递信号（跳转前往指定路由）
      this.$emit("routerPush", {path: item.path})
    },

    deepRouter() { // 前往更深一层的路由
      this.$emit("pathDeep", {path: this.path.path, nowDeep: this.nowDeep})
    },
    pathDeep(item) { // 用于递归向上传递信号（前往更深一层的路由）
      this.$emit("pathDeep", {path: item.path, nowDeep: item.nowDeep})
    },

    compare(s1, s2) {
      if (s1.length > s2.length) {
        if (s1.substring(0, s1.length - 1) == s2) {
          return true
        } else {
          return false
        }
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.router-item-content {
  text-align: center;
  color: #fff;
  width: 200px;
  height: 50px;
  line-height: 50px;
  margin: 0 auto;
  background-color: rgba($color: #6dc8fa, $alpha: 0.8);
  margin-bottom: 10px;
  display: flex;
  align-content: center;
  justify-content: center;
  position: relative;
  border-radius: 8px;
  box-shadow: 0 0 4px 0 rgb(0 0 0 / 10%);
  cursor: pointer;
  font-size: 18px;
}

.deep-btn {
  position: absolute;
  right: 10px;
}
</style>
